<?php
/**
 * 权限认证分配
 * 
 * @author  Lee <a605333742@gmail.com>
 * @time    2015-07-24
 */
namespace Admin\Controller;
//引用分页类
use Think\Page;

class CommonController extends EmptyController{
    /*
     *构造函数
     * 
     * @return # 
     */
    public function _initialize(){
        //第一步：判断是否开启了权限认证；判断当前控制器是否不需要认证
        if(C("SESSION_AUTO_START") && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))){
            if(!isset($_SESSION[C('USER_AUTH_KEY')])){
                $this->redirect(C('USER_AUTH_GATEWAY'));
            }else{
                if($_SESSION['type']==3){
                    $map['use_level']   =array('egt',1);                            
                }elseif($_SESSION['type']==2){
                    $map['use_level']   =array("egt",2);
                }else{
                    $map['use_level']   =3;
                }
                $controller=D("Node")->where($map)->field("controller")->select();
                $data   =array();
                foreach($controller as $k){
                    $data[] =$k['controller'];
                }
                if(CONTROLLER_NAME!=Index &&!in_array(CONTROLLER_NAME,$data)){
                    $this->error("无权进行此操作");
                }
            }         
        }
    }

    /*
     * 根据表单生成查询条件-进行列表过滤
     *
     * @param string $name 数据对象名称
     *
     * @return array
     */
    public function _search($name=''){
        //生成查询条件
        if (empty ($name)) {
            $name = CONTROLLER_NAME;
        }
        $model      =D( $name );        
        $map        =array ();
        $fieldArray =array();
        if ($model->getDbFields()){
            $fieldArray = $model->getDbFields();
        }
        if ($model->viewFields){
            foreach ( $model->viewFields as $key => $val ){
                $fieldArray =array_merge($fieldArray, $model->viewFields[$key]);
            }
            unset($fieldArray['_on']);
        }
        foreach ( $fieldArray as $key => $val ) {				
            if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != ''){
                //特别指定一些字段进行模糊查询
                $likeArray = array(
                    'id',
                    'user_id',
                    'sec_name',
                    'name',
                    'title',
                    'username',
                    'nickName',
                    'companyName',
                    'remark',
                    'content',
                    'theme',
                    'section_id',
                    'desc',
                    'accept_id',
                    'subjects',
                    'level_name',
                    'node_name',
                    'controller',
                    'action',
                    'result',
                    'job_number'
                );
                if (in_array($val, $likeArray)){
                    //模糊查询
                    $map [$val] = array('like', '%'.$_REQUEST [$val].'%');
                } else {
                    //精确查询
                    $map [$val] = $_REQUEST [$val];
                }
            }
        }
        return $map;
    }
    
    /*
     * 根据表单生成查询条件-进行列表过滤
     *
     * @param object 	$model 		数据对象
     * @param array 	$map 		过滤条
     * @param string 	$sortBy 	排序
     * @param boolean 	$asc 		是否正序
     * @param string 	$countPk 	主键
     * @param string 	$field 		提取字段
     *
     * @return array    #
     */
    public function _list($model, $map, $sortBy='',$asc =TRUE, $countPk="id", $field="*") {
        //验证传参的有效性
        if (empty($asc)){
                $asc = false;
        }
        if (isset($model->pk)) {
                $countPk = $model->pk;
        }
        if (empty($countPk)){
                $countPk = "id";
        }
        //排序字段 默认为主键名
        if (!empty ( $_REQUEST ['_order'] )) {
                $order = $_REQUEST ['_order'];
        } else {
                $order = ! empty ( $sortBy ) ? $sortBy : $countPk;
        }
        //排序方式默认按照倒序排列
        //接受 sort参数 0 表示倒序 非0都 表示正序
        if (!empty ( $_REQUEST ['_sort'] )) {
                $sort = $_REQUEST ['_sort'] == 'asc' ? 'asc' : 'desc'; //zhanghuihua@msn.com
        }else{
                $sort = $asc? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        $count = $model->where ( $map )->count ();
        if ($count > 0) {
            //创建分页对象            
            if($_REQUEST['numPerPage']){
                $_REQUEST ['listRows']=I("request.numPerPage");
            }
            if (! empty ( $_REQUEST ['listRows'] )) {
                    $listRows   =$_REQUEST ['listRows'];
            } else {
                    $listRows   =20;
            }
            $p = new Page ( $count, $listRows );
            //分页查询数据
            $voList = $model->where($map)->group($countPk)->field($field)->order("`" .$order ."` ". $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
            //echo $model->getlastsql();
            //echo $model->getlastsql();
            //分页跳转的时候保证查询条件            
            foreach ( $map as $key => $val ) {
                if (! is_array ( $val )) {
                    $p->parameter .= "$key=" . urlencode ( $val ) . "&";
                }
            }
            //分页显示
            $page = $p->show ();
            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式
            //模板赋值显示
            $this->assign ( 'list', $voList );
            $this->assign ( 'sort', $sort );
            $this->assign ( 'order', $order );
            $this->assign ( 'sortImg', $sortImg );
            $this->assign ( 'sortType', $sortAlt );
            $this->assign ( "page", $page );
        }
        //zhanghuihua@msn.com
        $this->assign ( 'totalCount', $count );
        $this->assign ( 'numPerPage', $p->listRows );
        $this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);
        return $voList;
    }  
    
    /*
     * 处理提交时间范围查询
     *
     * @param  string $startTimeString 开始时间表单名
     * @param  string $endTimeString   结束时间表彰名
     * 
     * @return array                   数据库查询条件
     */
    public function processTimeMap( $startTimeString, $endTimeString ){
        //没有设置就默认1星期前
        $startTime  =!empty( $_REQUEST[$startTimeString] ) ? strtotime( $_REQUEST[$startTimeString] ) : time() - 604800 ; 
        //没有设置就默认当前时间
        $endTime    =!empty( $_REQUEST[$endTimeString] ) ? strtotime( $_REQUEST[$endTimeString] ) : time();
        if( $endTime < $startTime ){
            $this->error( '结束时间不能早于开始时间!' );
        }
        return array( 'between', array( $startTime, $endTime ) );
    }    
}